package com.example.springboot.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.common.CcLogs;
import com.example.springboot.common.LogType;
import com.example.springboot.common.Result;
import com.example.springboot.entity.Goods;
import com.example.springboot.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 商品表前端操作接口
 **/
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    GoodsService goodsService;

    /**
     * 新增
     */
    @PostMapping("/add")
    @CcLogs(operation = "商品信息",type = LogType.ADD)
    public Result add(@RequestBody Goods goods) {
        try {
            goodsService.save(goods);
        } catch (Exception e) {
            if (e instanceof DuplicateKeyException) {
                return Result.error("插入数据错误");
            } else {
                return Result.error("系统错误");
            }
        }
        return Result.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    @CcLogs(operation = "商品信息",type = LogType.DELETE)
    public Result deleteById(@PathVariable Integer id) {
        goodsService.removeById(id);
        return Result.success();
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/delete/double")
    @CcLogs(operation = "商品信息",type = LogType.DOUBLEDELETE)
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        goodsService.removeBatchByIds(ids);
        return Result.success();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    @CcLogs(operation = "商品信息",type = LogType.UPDATE)
    public Result updateById(@RequestBody Goods goods) {
        goodsService.updateById(goods);
        return Result.success();
    }

    /**
     * 根据ID查询
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Goods goods = goodsService.getById(id);
        return Result.success(goods);
    }

    /**
     * 查询所有
     */
    @GetMapping("/selectAll")
    public Result selectAll(Goods goods) {
        List<Goods> goodsList = goodsService.list(new QueryWrapper<Goods>().orderByDesc("id"));
        return Result.success(goodsList);
    }

    /**
     * 分页查询
     */
    @GetMapping("/selectByPage")
    public Result selectPage(@RequestParam String name,
                             @RequestParam Integer pageNum,
                             @RequestParam Integer pageSize) {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<Goods>().orderByDesc("id");
        queryWrapper.like(StrUtil.isNotBlank(name), "name", name);
        Page<Goods> page = goodsService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return Result.success(page);
    }

}